package com.kantboot.functional.im.listener;

import com.alibaba.fastjson2.JSON;
import com.kantboot.functional.im.domain.entity.FunctionalImPushMessage;
import com.kantboot.functional.im.domain.entity.FunctionalImPushMessageBroker;
import com.kantboot.functional.im.domain.entity.FunctionalImWebsocket;
import com.kantboot.functional.im.dto.FunctionalImPushMessageDTO;
import com.kantboot.functional.im.event.ImPushEvent;
import com.kantboot.functional.im.server.WebsocketServer;
import com.kantboot.functional.im.service.IFunctionalImPushMessageService;
import com.kantboot.functional.im.service.IFunctionalImWebSocketService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class ImPushEventListenerOfWebsocket {

    @Resource
    private IFunctionalImWebSocketService service;

    @Resource
    private IFunctionalImPushMessageService pushMessageService;

    @Resource
    private WebsocketServer websocketServer;

    /**
     * 监听到消息后，将消息推送到websocket
     */
    @EventListener
    public void onApplicationEvent(ImPushEvent event) {
        FunctionalImPushMessage message = event.getMessage();
        FunctionalImPushMessageBroker broker = new FunctionalImPushMessageBroker();
        // 循环得到message的中间件
        List<FunctionalImPushMessageBroker> brokers = message.getBrokers();
        if (null == brokers || brokers.size() == 0) {
            log.info("没有选定任何中间件，不推送Websocket消息");
            return;
        }
        for (FunctionalImPushMessageBroker functionalImPushMessageBroker : brokers) {
            if (functionalImPushMessageBroker.getCode().equals("websocket")) {
                broker = functionalImPushMessageBroker;
            }
        }

        if (null == broker) {
            log.info("没有选定websocket中间件，不推送Websocket消息");
            return;
        }

        log.info("选定了websocket中间件，正在推送Websocket消息");

        // 判断推送类型，如果是推送到指定的CID，则推送到指定的CID
        if (FunctionalImPushMessageDTO.PUSH_TYPE_CODE_CID.equals(message.getPushTypeCode())) {
            log.info("推送类型为推送到指定的CID");
            // 推送到指定的客户端
            websocketServer.sendMessageByCid(broker.getCid(), JSON.toJSONString(message));
            // 将消息的状态改为已发送
            pushMessageService.changeStatusCode(broker.getId(), FunctionalImPushMessageDTO.Broker.STATUS_CODE_SEND_SUCCESS);
            return;
        }
        if (FunctionalImPushMessageDTO.PUSH_TYPE_CODE_USER_ID.equals(message.getPushTypeCode())) {
            log.info("推送类型为推送到指定的用户");
            List<FunctionalImWebsocket> allByUserId = service.getAllOpenedByUserId(message.getUserId());
            for (FunctionalImWebsocket functionalImWebsocket : allByUserId) {
                websocketServer.sendMessageByCid(functionalImWebsocket.getCid(), JSON.toJSONString(message));
                // 将消息的状态改为已发送
                pushMessageService.changeStatusCode(broker.getId(), FunctionalImPushMessageDTO.Broker.STATUS_CODE_SEND_SUCCESS);
            }
        }
    }

}
